package red.lixiang.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class IS_ListUtils {

    /**
     * List根据某一个属性去重
     *        List<ProductAttribute> colorList
     *        =allAttr.stream()
     *          .filter(IS_ListUtils.distinctByKey(ProductAttribute::getColorCode))
     *          .collect(Collectors.toList());
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
